<template>
	<view class="columnbox wdh-100 content">
		<view class="info-box wdh-100 columnbox">
			<view class="img-list wdh-100">
				<view class="rowbox img-box" v-for="(item,index) in imgList" :key='index'>
					<view @click="deletImg(index)" class="img-close rowbox">
						<u-icon name="close" size="24rpx" color="#fff"></u-icon>
					</view>
					<image class="goods-img" :src="item.url" mode="aspectFill"></image>
				</view>
				<view class="rowbox add-img" @click="addImg">
					<u-icon name="plus" size="32rpx" color="#1f1f1f"></u-icon>
				</view>
			</view>
			<view class="rowbox wdh-100 one-info spb">
				<view>商品名称</view>
				<input v-model="name" placeholder-class="noinfo" placeholder="点击输入" />
			</view>
			<view class="rowbox wdh-100 one-info spb">
				<view>商品分类</view>
				<picker mode="selector" :range="typeList" range-key="name" @change="chooseType">
					<view class="rowbox noinfo" v-if="curtype===''">
						选择 <view style="margin-left: 10rpx;"><u-icon name="arrow-right" size="24rpx"
								color="#d3d3d5"></u-icon></view>
					</view>
					<view class="rowbox" v-else>
						{{typeList[curtype].name}}
						<view style="margin-left: 10rpx;"><u-icon name="arrow-right" size="24rpx" color="#000"></u-icon>
						</view>
					</view>
				</picker>
			</view>
			<view class="columnbox wdh-100 one-info">
				<view class="rowbox wdh-100 spb">
					<view>商品规格</view>
					<view class="def-col" @click="addSpec">
						+ 新增规格
					</view>
				</view>
				<view class="columnbox wdh-100 spb one-spec" v-for="(item,index) in specList" :key='index'>
					<view class="rowbox wdh-100 spb">
						<view>{{item.name}}</view>
						<u-icon name="close-circle-fill" size="32rpx" @click="delGuige(item,index)"></u-icon>
					</view>
					<view class="rowbox wdh-100 spb spec-box">
						<text>现价：¥{{item.price}}</text>
						<text>原价：¥{{item.original}}</text>
						<!-- <text>单位：{{item.danwei}}</text> -->
					</view>
				</view>
			</view>
		</view>
		<view class="bottom-box wdh-100 rowbox">
			<view v-if="submitflag" class="one-btn rowbox submit-btn">保存</view>
			<view v-else @click="addGoods" class="one-btn rowbox def-bgcol">保存</view>
		</view>
		<u-popup mode="center" :show="specflag" round="10rpx">
			<view class="spec-info columnbox">
				<view>添加规格</view>
				<view class="rowbox wdh-100 one-info spb">
					<view>规格名称</view>
					<input v-model="curspec.name" placeholder-class="noinfo" placeholder="点击输入" />
				</view>
				<view class="rowbox wdh-100 one-info spb">
					<view>售价</view>
					<input type="digit" v-model="curspec.price" placeholder-class="noinfo" placeholder="点击输入" />
				</view>
				<view class="rowbox wdh-100 one-info spb">
					<view>原价</view>
					<input type="digit" v-model="curspec.original" placeholder-class="noinfo" placeholder="点击输入" />
				</view>
				<!-- <view class="rowbox wdh-100 one-info spb">
					<view>单位</view>
					<input v-model="curspec.danwei" placeholder-class="noinfo" placeholder="点击输入" />
				</view> -->
				<view class="rowbox wdh-100 spb">
					<view class="rowbox cancel-btn" @click="specflag=false">取消</view>
					<view class="rowbox confirm-btn def-bgcol" @click="addOneSpec">确定</view>
				</view>
			</view>
		</u-popup>
	</view>
</template>

<script>
	export default {
		data() {
			return {
				submitflag: false,
				specflag: false,
				imgList: [],
				specList: [],
				curspec: {
					price: '',
					name: '',
					original: '',

				},
				typeList: [],
				curtype: '',
				name: "",
				uploadimglist: [],
				id: 0,
				updateflag: false
			}
		},
		onLoad(options) {
			if (options.id) {
				this.id = parseInt(options.id)
				this.updateflag = true
			}
			this.getType()
		},
		methods: {
			delGuige(data, index) {
				this.specList.splice(index, 1)
			},
			getGoodsDetail() {
				this.$req.post('/shanghu/goodinfo', {
					id: this.id
				}).then(res => {
					let info = res.data.data
					this.name = info.name
					info.image.split(',').forEach(img => {
						this.imgList.push({
							url: img,
							newflag: false
						})
					})
					if (this.typeList.length > 0) {
						this.typeList.forEach((type, index) => {
							if (type.id == info.goodtypdid) {
								this.curtype = index
							}
						})
					}
					this.specList = info.List
				})
			},
			addGoods() {
				if (this.imgList.length == 0) {
					uni.showToast({
						title: '请上传商品图片',
						icon: 'none'
					})
				} else if (this.name == '') {
					uni.showToast({
						title: '请输入商品名称',
						icon: 'none'
					})
				} else if (this.curtype === '') {
					uni.showToast({
						title: '请选择商品分类',
						icon: 'none'
					})
				} else if (this.specList.length == 0) {
					uni.showToast({
						title: '请添加商品规格',
						icon: 'none'
					})
				} else {
					if (!this.submitflag) {
						this.submitflag = true
						uni.showLoading({
							title: '正在保存'
						})
						if (this.updateflag) {
							this.updateInfo()
						} else {
							this.submitInfo()
						}
					}
				}
			},
			async updateInfo() {
				for (var i = 0; i < this.imgList.length; i++) {
					await this.uploadImg(this.imgList[i])
				}
				this.$req.post('/shanghu/goodupdate', {
					id: this.id,
					goodtypdid: parseInt(this.typeList[this.curtype].id),
					name: this.name,
					image: this.uploadimglist.join(','),
					parameter: JSON.stringify(this.specList)
				}).then(res => {
					uni.hideLoading()
					let pages = getCurrentPages(); //获取所有页面栈实例列表
					let nowPage = pages[pages.length - 1]; //当前页页面实例
					let prevPage = pages[pages.length - 2]; //上一页页面实例
					prevPage.$vm.refreshflag = true
					uni.navigateBack()
				})
			},
			async submitInfo() {
				for (var i = 0; i < this.imgList.length; i++) {
					await this.uploadImg(this.imgList[i])
				}
				this.$req.post('/shanghu/goodadd', {
					goodtypdid: parseInt(this.typeList[this.curtype].id),
					name: this.name,
					image: this.uploadimglist.join(','),
					parameter: JSON.stringify(this.specList)
				}).then(res => {
					uni.hideLoading()
					let pages = getCurrentPages(); //获取所有页面栈实例列表
					let nowPage = pages[pages.length - 1]; //当前页页面实例
					let prevPage = pages[pages.length - 2]; //上一页页面实例
					prevPage.$vm.refreshflag = true
					uni.navigateBack()
				})
			},
			uploadImg(url) {
				return new Promise((resolve, reject) => {
					if (url.newflag) {
						this.$req.uploadImg(url.url).then(res => {
							this.uploadimglist.push(res.data.data)
							resolve(true)
						})
					} else {
						this.uploadimglist.push(url.url)
						resolve(true)
					}

				})
			},
			chooseType(e) {
				this.curtype = e.detail.value
			},
			getType() {
				this.$req.post('/shanghu/goodtype', {
					shanghuid: uni.getStorageSync('userInfo').shanghuid
				}).then(res => {
					this.typeList = res.data.data
					if (this.updateflag) {
						this.getGoodsDetail()
					}
				})
			},
			addImg() {
				uni.chooseImage({
					success: (res) => {
						res.tempFilePaths.forEach(img => {
							this.imgList.push({
								newflag: true,
								url: img
							})
						})
					}
				})
			},
			deletImg(index) {
				this.imgList.splice(index, 1)
			},
			addSpec() {
				this.specflag = true
				this.curspec = {
					price: '',
					name: '',
					original: '',

				}
			},
			addOneSpec() {
				if (this.curspec.name == '') {
					uni.showToast({
						title: '请输入规格名称',
						icon: 'none'
					})
				} else if (this.curspec.price == '') {
					uni.showToast({
						title: '请输入售价',
						icon: 'none'
					})
				} else if (this.curspec.original == '') {
					uni.showToast({
						title: '请输入原价',
						icon: 'none'
					})
				} else {
					this.specflag = false
					this.specList.push({
						price: parseFloat(this.curspec.price),
						name: this.curspec.name,
						original: parseFloat(this.curspec.original),

					})
				}
			}
		}
	}
</script>

<style lang="scss" scoped>
	.content {}

	.info-box {
		padding: 20rpx 20rpx;
	}

	.img-list {
		display: flex;
		flex-wrap: wrap;
		justify-content: flex-start;
	}

	.add-img {
		width: 150rpx;
		height: 150rpx;
		border: #b1b3b9 dotted 1px;
		border-radius: 10rpx;
		margin-right: 20rpx;
	}

	.img-box {
		position: relative;
		width: 150rpx;
		height: 150rpx;
		margin-right: 20rpx;
	}

	.img-close {
		position: absolute;
		top: -10rpx;
		right: -10rpx;
		width: 32rpx;
		height: 32rpx;
		border-radius: 100rpx;
		background: rgba(0, 0, 0, 0.7);
	}

	.goods-img {
		width: 150rpx;
		height: 150rpx;
		border-radius: 10rpx;
	}

	.one-info {
		background: #f7f8fa;
		padding: 20rpx 32rpx;
		margin-top: 20rpx;
		border-radius: 10rpx;
		font-size: 28rpx;
		line-height: 70rpx;

		input {
			text-align: right;
			width: calc(100% - 200rpx);
			font-size: 28rpx;
		}
	}

	.bottom-box {
		position: fixed;
		bottom: 0;
		left: 0;
		z-index: 10;
		background: #fff;
		padding: 20rpx 20rpx;
		padding-bottom: 40rpx;
		box-shadow: 0 0 4px 2px #eeecef;
	}

	.one-btn {
		width: 100%;
		font-size: 28rpx;
		height: 90rpx;
		border-radius: 20rpx;
		color: #fff;
	}

	.one-spec {
		margin-top: 20rpx;
		border-top: #eeecef solid 1px;
	}

	.spec-box {
		text {
			width: 33%;
		}
	}

	.spec-info {
		width: 600rpx;
		padding: 20rpx 20rpx;
	}

	.cancel-btn {
		margin-top: 20rpx;
		width: 250rpx;
		height: 90rpx;
		border-radius: 10rpx;
		color: #606266;
		border: #eeecef solid 1px;
	}

	.confirm-btn {
		margin-top: 20rpx;
		width: 250rpx;
		height: 90rpx;
		border-radius: 10rpx;
		color: #fff;
	}

	.submit-btn {
		background: #eeecef;
		color: #606266;
	}
</style>
<style>
	.noinfo {
		color: #a7a8aa;
	}
</style>